/**
* 版权声明：睿商(厦门)科技股份有限公司 版权所有 违者必究
* 日    期：2021-07-06
*/
package com.rzico.admin.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.admin.model.ProjectStatistics;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.core.entity.SysUser;
import com.rzico.core.service.SysUserService;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.admin.entity.Project;
import com.rzico.admin.service.ProjectService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;

import java.util.*;

/**
 * 项目登记表控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2021-07-06
 */
@Api(description = "项目登记表接口")
@RestController
@RequestMapping("/admin/project")
public class ProjectAdminController extends BaseController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 分页查询项目登记表
     *
     * @return
    */
    @ApiOperation("分页查询项目登记表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司ID", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "搜索关键词", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "status", value = "状态(0.草稿,1.提交,2.审核,3.驳回)", dataType = "Integer", paramType = "query")
    })
    public CommResult<Project> list(Long companyId,Date startDate, Date endDate,Integer status,String keyword, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
           params.put("startDate", startDate);
        }
        if (endDate!=null){
           params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }

        if (status!=null){
            params.put("status", status);
        }

        if (companyId!=null) {
            params.put("companyId", companyId);
        }


        if (keyword!=null){
            params.put("keyword", keyword);
        }



        SysUser sysUser = sysUserService.getCurrent();
        if (!sysUserService.isAdmin(sysUser)) {
            params.put("dataScopeId", sysUser.getId());
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Project> list = projectService.selectList(params);
        PageResult<Project> pageResult = new PageResult<Project>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条项目登记表
     *
     * @return
    */
    @ApiOperation("查询单条项目登记表")
    @GetMapping("/find/{id}")
    public CommResult<Project> find(@PathVariable String id) {
        Project result = projectService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("project",result);
        return CommResult.success(data);

    }

    /**
     * 保存项目登记表
     *
     * @param project
     * @return
     */
    @ApiOperation("保存项目登记表")
    @PostMapping("/save")
    public CommResult<Project> save(@RequestBody Project project) {
        int affectCount = projectService.insert(project);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        Project result = projectService.findById(project.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除项目登记表
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除项目登记表", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除项目登记表,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<Project> del(@PathVariable String ids) {

        projectService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新项目登记表
     *
     * @param project
     * @return
     */
    @Log(desc = "更新项目登记表", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新项目登记表")
    @PostMapping("/update")
    public CommResult<Project> update(@RequestBody Project project) {
        int affectCount = projectService.updateByPrimaryKeySelective(project);
        if (affectCount <= 0){
            return CommResult.error();
        }
        Project result = projectService.findById(project.getId());
        return CommResult.success(result);
    }

    /**
     * 统计项目登记表
     *
     * @return
     */
    @ApiOperation("统计项目项目登记表")
    @GetMapping("/statistics")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态(0.草稿,1.提交,2.审核,3.驳回)", dataType = "Integer", paramType = "query")
    })
    public CommResult<ProjectStatistics> statistics(Date startDate,Integer status) {
        Map<String, Object> params = new HashMap<String, Object>();

        if (startDate!=null){
            startDate = DateUtils.truncate(startDate, Calendar.YEAR);
            params.put("startDate", startDate);

            Date endDate = DateUtils.addSeconds(DateUtils.addYears(startDate, 1),-1);
            params.put("endDate", endDate);

        }

        if (status!=null){
            params.put("status", status);
        }

        params.put("ProjectStatistics","1");

        ProjectStatistics projectStatistics = projectService.selectProjectStatistics(params);
        if(projectStatistics==null){
            CommResult.error();
        }
        return CommResult.success(projectStatistics);
    }

    /**
     * 提交项目登记
     * @param projectId
     * @return
     */
    @ApiOperation("提交项目登记")
    @PostMapping("/submit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目登记id", dataType = "Long", paramType = "query")
    })
    public CommResult<Project> submit(Long projectId) {
        int affectCount = projectService.submit(projectId);
        if (affectCount <= 0){
            return CommResult.error();
        }
        return CommResult.success();
    }

    /**
     * 审核项目登记
     * @param projectId
     * @return
     */
    @ApiOperation("审核项目登记")
    @PostMapping("/adopt")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目登记id", dataType = "Long", paramType = "query")
    })
    public CommResult<Project> adopt(Long projectId) {
        int affectCount = projectService.adopt(projectId);
        if (affectCount <= 0){
            return CommResult.error();
        }
        return CommResult.success();
    }

    /**
     * 驳回项目登记
     * @param projectId
     * @return
     */
    @ApiOperation("驳回项目登记")
    @PostMapping("/reject")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目登记id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "rejectText", value = "驳回理由", dataType = "String", paramType = "query")
    })
    public CommResult<Project> reject(Long projectId,String rejectText) {
        int affectCount = projectService.reject(projectId,rejectText);
        if (affectCount <= 0){
            return CommResult.error();
        }
        return CommResult.success();
    }
}
